package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.OrderLogMapper;
import com.etalk.crm.pojo.OrderLog;
import com.etalk.crm.service.OrderLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 订单操作记录
 *
 * @author Terwer
 * @version 1.0
 * 2019/6/14 15:34
 **/
@Service
public class OrderLogServiceImpl implements OrderLogService {
    private final Logger logger = LogManager.getLogger(this.getClass());

    @Resource
    private OrderLogMapper orderLogMapper;

    /**
     * 查询订单操作日志列表
     *
     * @param paramMap 参数
     * @return 结果
     */
    @Override
    public PageInfo<OrderLog> getOrderLogList(Map<String, Object> paramMap, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderLog> list = orderLogMapper.selectOrderLogList(paramMap);
        //分页信息
        PageInfo<OrderLog> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        if (pageNum > pages) {
            return new PageInfo<>();
        }
        pageNum = pageInfo.getPageNum();
        pageSize = pageInfo.getPageSize();
        logger.info("分页信息：total=" + total + "，pages=" + pages + "，pageNum=" + pageNum + "，pageSize=" + pageSize);
        return pageInfo;
    }

    /**
     * 添加订单操作日志记录
     *
     * @param orderLog 操作日志记录
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrderLog(OrderLog orderLog) {
        orderLogMapper.addOrderLog(orderLog);
    }
}
